//
//  JNTMoreViewController.m
//  CRSCMOA
//
//  Created by ANGELINE on 16/8/11.
//  Copyright © 2016年 王文晓. All rights reserved.
//

#import "JNTMoreViewController.h"
#import "PersonalNeedCollectionViewCell.h"
#import "homeModel.h"

@interface JNTMoreViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIGestureRecognizerDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) NSMutableArray *addDataSourceArray;
@property (nonatomic, strong) NSMutableArray *addImageArray;

@property (nonatomic, strong) UILongPressGestureRecognizer *longResture;

@end

@implementation JNTMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
    [self setUpNavgationBar];
    
    [self.view addSubview:self.collectionView];
    
    self.longResture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGestureAction:)];
    self.longResture.delegate = self;
//    self.longResture.delaysTouchesBegan = YES;
//    [self.collectionView addGestureRecognizer:self.longResture];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(backAction:)];
}


- (void)backAction:(UIBarButtonItem *)sender {
    
    
    if ([self.delegate respondsToSelector:@selector(passValue:)]) {
        
        [self.delegate passValue:self.addDataSourceArray];
        [self.delegate passImage:self.addImageArray];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSourceArray.count;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    PersonalNeedCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PersonalNeedCell" forIndexPath:indexPath];
    cell.cellLabel.text = self.dataSourceArray[indexPath.row];
    cell.cellImageView.image = [UIImage imageNamed:[self.imageArray objectAtIndex:indexPath.row]];

    self.longResture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longGestureAction:)];
    self.longResture.delaysTouchesBegan = YES;
    [cell addGestureRecognizer:self.longResture];
    
    return cell;
    
}


- (void)longGestureAction:(UILongPressGestureRecognizer *)sender {

    if (self.dataSourceArray.count > 0) {
        
        if (sender.state == UIGestureRecognizerStateEnded) {
            
            CGPoint p = [sender locationInView:self.collectionView]; //获取当前点击cell的位置
            NSIndexPath *indexpath = [self.collectionView indexPathForItemAtPoint:p];//获取cell并得到对应的indexpath;
            if (indexpath == nil) {
                
                NSLog(@"没有item");
            }else {
                
                //获取cell
                PersonalNeedCollectionViewCell *cell = (PersonalNeedCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexpath];
                [self.addDataSourceArray addObject:cell.cellLabel.text];
                [self.addImageArray addObject:[self.imageArray objectAtIndex:indexpath.row]];
                [self.dataSourceArray removeObjectAtIndex:indexpath.row];
                NSArray *deleteItems = @[indexpath];
                [self.collectionView deleteItemsAtIndexPaths:deleteItems];
            }
        }
    }else {
        
        [self.imageArray removeAllObjects];
    }
            
    [self.collectionView reloadData];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 设置导航条
- (void)setUpNavgationBar
{
    self.title = @"增加功能";
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
    self.navigationController.navigationBar.translucent = NO;
    
}


#pragma mark - -----laying-------
- (UICollectionView *)collectionView {
    
    if (!_collectionView) {
        
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.itemSize = CGSizeMake((kScreenWidth - 2)/4, kScreenWidth/4);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 1, 1);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:flowLayout];
        [_collectionView registerNib:[UINib nibWithNibName:@"PersonalNeedCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"PersonalNeedCell"];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
    }
    return _collectionView;
}

- (NSMutableArray *)dataSourceArray {
    
    if (!_dataSourceArray) {
        
        _dataSourceArray = [NSMutableArray arrayWithObjects:@"访客",@"工作任务",@"日程",@"工作汇报",@"停车",@"用户中心",@"公告栏",@"门禁", nil];
    }
    return _dataSourceArray;
}

- (NSMutableArray *)imageArray {
    
    if (!_imageArray) {
        
        _imageArray = [NSMutableArray arrayWithObjects:@"home_btn_visitor_f",@"home_btn_package_f",@"home_btn_schedule_f",@"home_btn_report_f",@"home_btn_parking_f",@"home_btn_personal_f",@"home_btn_news_approval_f",@"ic_meetingroom", nil];
    }
    return _imageArray;
}

- (NSMutableArray *)addDataSourceArray {
    
    if (!_addDataSourceArray) {
        
        _addDataSourceArray = [[NSMutableArray alloc] init];
    }
    return _addDataSourceArray;
}
- (NSMutableArray *)addImageArray {
    
    if (!_addImageArray) {
        
        _addImageArray = [[NSMutableArray alloc] init];
    }
    return _addImageArray;
}
@end
